package com.wisemoresystem.seal.service;

import java.util.List;
import java.util.Map;

import org.ne.framework.data.util.ChkTools;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wisemoresystem.seal.base.service.BaseService;
import com.wisemoresystem.seal.model.t_file;
import com.wisemoresystem.seal.model.t_location;
import com.wisemoresystem.seal.web.WebTools;

@Service
public class LocationService extends BaseService{

	public List<Map<String, Object>> selectLocation(){
		String sql = "select * from t_location t where t.status=0 and t.name='中国行政区' ";
		List<Map<String, Object>> list = jt.queryForList(sql);
		for (Map<String, Object> map : list) {
			String id = map.get("_id")+"";
			int count = getChildrenCount(id);
			map.put("count", count);
			if(count!=0){
				map.put("isParent", true);
			}else{
				map.put("isParent", false);
			}
		}
		return list;
	}
	
	public t_location selectLocationById(String location_id) {
		String hql = "from t_location t where t._id=? and t.status=0";
		t_location t = super.findObj(hql, location_id);
		return t;
	}

	public t_file selectFileById(String file_id){
		String hql = "from t_file t where t._id=? and t.status=0";
		t_file t = super.findObj(hql, file_id);
		return t;
	}
	
	@Transactional
	public void saveLocation(List<Map<String, Object>> list) {
		// TODO Auto-generated method stub
		for (Map<String, Object> map : list) {
			String super_code = map.get("super_code")+"";
			t_location t = selectLocationByCode(super_code);
			if(ChkTools.isNotNull(t)){
				t_location loc = selectLocationByCode(map.get("code")+"");
				if(ChkTools.isNotNull(loc)){
					loc.setName(map.get("name")+"");
					loc.setSuper_id(t.get_id());
					super.updateObj(loc);
				}else{
					loc = new t_location();
					loc.setCode(map.get("code")+"");
					loc.setName(map.get("name")+"");
					loc.setSuper_id(t.get_id());
					loc.setUser_id(WebTools.getCurrentUserId());
					super.saveObj(loc);
				}
			}else{
				t = new t_location();
				t.setCode(map.get("super_code")+"");
				t.setUser_id(WebTools.getCurrentUserId());
				t.setSuper_id("root");
				super.saveObj(t);
				t_location loc = new t_location();
				loc.setCode(map.get("code")+"");
				loc.setName(map.get("name")+"");
				loc.setUser_id(WebTools.getCurrentUserId());
				loc.setSuper_id(t.get_id());
				super.saveObj(loc);
			}
		}
	}
	
	public t_location selectLocationByCode(String code){
		String hql = "from t_location t where t.code=? and t.status=0";
		t_location t = super.findObj(hql, code);
		return t;
	}
	
	/**
	 * 异步加载地区
	 * @return
	 */
	public List<Map<String, Object>> selectLocationByAsync(String location_id){
		String sql = "select t._id zid,t.* from t_location t where t.status=0 and t.super_id=? order by t.name asc ";
		List<Map<String, Object>> list = jt.queryForList(sql,location_id);
		for (Map<String, Object> map : list) {
			String id = map.get("_id")+"";
			int count = getChildrenCount(id);
			map.put("count", count);
			if(count!=0){
				map.put("isParent", true);
			}else{
				map.put("isParent", false);
			}
		}
		return list;
	}
	
	private int getChildrenCount(String id){
		String sql = "select * from t_location t where t.status=0 and t.super_id=? order by t.name asc ";
		List<Map<String, Object>> list = jt.queryForList(sql,id);
		return list.size();
	}
	
	/**
	 * 是否存在下级地区
	 * @param id
	 * @return
	 */
	public boolean isLive(String id){
		String sql = "select * from t_location t where t.status=0 and t.super_id=? order by t.name asc ";
		List<Map<String, Object>> list = jt.queryForList(sql,id);
		if(ChkTools.isNotNull(list))
			return true;
		return false;
	}
}
